package com.ruoyi.sumfun.blog.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.sumfun.blog.service.ArticleService;
import com.ruoyi.system.api.domain.blog.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 文章Controller
 *;'
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "文章信息Controller" , tags = {"文章信息" })
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {
    @Autowired
    private ArticleService articleService;

/**
 * 查询文章信息列表
 */
//@RequiresPermissions("system:info:list")
@ApiOperation("查询文章信息列表")
@GetMapping("/list")
    public TableDataInfo list(Article article) {
//        startPage();
        List<Article> list = articleService.selectList(article);

        return getDataTable(list);
    }

    /**
     * 查询文章数量，标签数量、分类数量
     */
//@RequiresPermissions("system:info:list")
    @ApiOperation("查询文章数量，标签数量、分类数量")
    @GetMapping("/count")
    public AjaxResult getDataCount() {

        // 创建一个Map来存储结果
        Map<String, Integer> counts = articleService.selectCount();

        return success(counts);
    }

    /**
     * 查询文章信息列表
     */
//@RequiresPermissions("system:info:list")
    @ApiOperation("数据中心查询文章信息列表")
    @GetMapping("/getDataList")

    public R getDataList(Article article) {

        List<Article> list = articleService.selectList(article);
        R result = new R();
        result.setCode(200);
        result.setMsg("查询成功");
        result.setData(list);
        return result;
    }


    /**
     * 导出文章信息列表
     */
    //@RequiresPermissions("system:info:export")
    @ApiOperation("导出文章信息列表")
    @Log(title = "文章信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article) {
        List<Article> list = articleService.selectList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article. class);
        util.exportExcel(response, list, "文章信息数据");
    }

    /**
     * 获取文章信息详细信息
     */
    //@RequiresPermissions("system:info:query")
    @ApiOperation("获取文章信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(articleService.selectById(id));
    }

    /**
     * 新增文章信息
     */
    @ApiOperation("新增文章信息")
    @Log(title = "新增文章信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article) {
        return toAjax(articleService.insert(article));
    }

    /**
     * 修改文章信息
     */
    //@RequiresPermissions("system:info:edit")
    @ApiOperation("修改文章信息")
    @Log(title = "文章信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article) {
        return toAjax(articleService.update(article));
    }

    /**
     * 删除文章信息
     */
    //@RequiresPermissions("system:info:remove")
    @ApiOperation("删除文章信息")
    @Log(title = "文章信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(articleService.deleteByIds(ids));
    }
}
